package model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

import view.SwingFrame;


public class Repertoire {
	private ArrayList<Contact> contacts;
	private SwingFrame frame;
	
	
	
	public Repertoire(int nbContacts, SwingFrame frame2) {
		this.frame=frame2;
		contacts=new ArrayList<Contact>();
		for(int i=0;i<nbContacts;i++){
			contacts.add(new Contact(i));
		}

	}

	public String showRepertoire(){ 
		Iterator<Contact> it = contacts.iterator();
		String fulltext="";
		while (it.hasNext()) {
			Contact current = it.next();
			fulltext+=current.showContact();
		}
		return fulltext;
	}
	
	public String showAlphaRepertoire(){ 
		Iterator<Contact> it = contacts.iterator();
		StringBuilder fulltext=new StringBuilder();
		char c='#';
		int i;
		while (it.hasNext()) {
			Contact current = it.next();
			if(current.getLastname().charAt(0)!=c){
				c=current.getLastname().charAt(0);
				for(i=0;i<20;i++){
					fulltext.append(c);
					fulltext.append("--");
				}
				fulltext.append(c).append("\n");
			}
			fulltext.append(current.showContact());
		}
		return fulltext.toString();
	}
	
	public Contact findContact(String name){
		for(Contact current:contacts){
			if(current.getLastname().equals(name)){
				return current;
			}
		}
		return null;
	}


	public String showContactsGroup(String groupby){
		StringBuilder fulltext=new StringBuilder();
		for(Contact current: contacts){
			if(current.getGroup().equals(groupby)){
				fulltext.append(current.showContact());
			}
		}
		
		return fulltext.toString();

	}

	public void addToRepertoire(String nfirstname,String nlastname,String naddress,String ncompanyaddress,int npersonalphone,int nprofessionalphone,String nhomepage,String nproemail,String nperemail,String ngroup,String nphoto){ 
		boolean truth=false;
		Contact current;
		for(int i=0;i<contacts.size();i++){
			current=contacts.get(i);
			if(current.getLastname().equals(nlastname)){
				truth=true;
				contacts.get(i).setAddress(naddress);
				contacts.get(i).setCompanyaddress(ncompanyaddress);
				contacts.get(i).setFirstname(nfirstname);
				contacts.get(i).setGroup(ngroup);
				contacts.get(i).setHomepage(nhomepage);
				contacts.get(i).setLastname(nlastname);
				contacts.get(i).setPeremail(nperemail);
				contacts.get(i).setPersonalphone(npersonalphone);
				contacts.get(i).setProemail(nproemail);
				contacts.get(i).setProfessionalphone(nprofessionalphone);
			}
		}
		
		if(truth==false){
			contacts.add(new Contact(nfirstname,nlastname,naddress,ncompanyaddress,npersonalphone,nprofessionalphone,nhomepage,nproemail,nperemail,ngroup,nphoto));
		}
	}

	public void save(){
		File save=new File("./res/save.csv");
		try {
			PrintWriter writer=new PrintWriter(save);

			Iterator<Contact> it = contacts.iterator();
			Contact current;
			writer.println("ID;FirstName;LastName;Address;CompanyAddress;PersonalPhone;ProfessionalPhone;Homepage;ProfessionalEmail;PersonalEmail;Group;Photo");
			while (it.hasNext()) {
				current = it.next();
				writer.println(current.saveCSVContact());
			}
			writer.flush();
			writer.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void deleteFromRepertoire(String regex){//http://open.spotify.com/track/4jYQI2JsitXwTNWfXOFOdB
		Contact current;
		for(int i=0;i<contacts.size();i++){
			current=contacts.get(i);
			if(current.getLastname().equals(regex)){
				contacts.remove(i);
			}
		}
	}
	public void load(){
		File save=new File("./res/save.csv");
		try {
			int ID=0;
			contacts=new ArrayList<Contact>();
			String current[] = null;
			Contact newContact;
			Scanner sc=new Scanner(save);
			sc.next();
			while(sc.hasNext()){
				current=sc.next().split(";");
				newContact=new Contact(
						current[1],
						current[2],
						current[3],
						current[4],
						Integer.parseInt(current[5]),
						Integer.parseInt(current[6]),
						current[7],
						current[8],
						current[9],
						current[10],
						current[11]
						);
				newContact.setID(ID);
				ID++;
				contacts.add(newContact);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void alphabetize(){
		Collections.sort(contacts,new Contact());
	}

	public String[] getNames(){
		String[]names=new String[contacts.size()];
		for(int i=0;i<contacts.size();i++){
			Contact current = contacts.get(i);
			names[current.getID()]=current.getLastname();
		}

		return names;
	}
	
	public String getPerson(String name){
		String person="";
		for(Contact current:contacts){
			if(current.getLastname().equals(name)){
				person=current.showContact();
			}
		}
		return person;
	}

	public String[] getGroups(){
		String[]groups=new String[contacts.size()];

		Iterator<Contact> it = contacts.iterator();
		while (it.hasNext()) {
			Contact current = it.next();
			groups[current.getID()]=current.getGroup();
		}
		groups = new HashSet<String>(Arrays.asList(groups)).toArray(new String[0]);

		return groups;
	}

	

}
